<?php
namespace app\admin\controller;

use app\common\controller\Back;

class User extends Back
{
    private $pageSize = 20;
    
    protected function initialize()
    {
        parent::initialize();
    }
    
    public function index()
    {
        $args = [
            'where' => [],
        ];
        //搜索
        if($this->query['searchText']){
            array_push($args['where'],['user_mobile|user_name|user_email','like','%'.$this->query['searchText'].'%']);
        }
        //排序
        if(in_array($this->query['sortOrder'],model('common/Sort','loglic')->user())){
            $args['order'] = [
                $this->query['sortOrder'] => 'desc',
            ];
        }else{
            $args['order'] = [
                'user_id' => 'desc',
            ];
        }
        //查询数据
        $list = model('common/User','loglic')->selectPage($args,$this->pageSize,$this->site['page'],false,xfUrlPath('admin/user/index',$this->query));
        // 获取分页显示
        //$total = $list->total();
        $pages = $list->render();
        // 模板变量赋值
        $this->assign('list', $list);
        $this->assign('pages', $pages);
        $this->assign('query', $this->query);
        $this->assign($this->site);
        // 渲染模板输出
        return $this->fetch();
    }
    
    public function create()
    {
        $form = xfFormCreate([
            'name'      => 'adminUserCreate',
            'action'    => xfUrl('admin/user/save'),
            'class'     => 'row',
            'reset'     => '重置',
            'items'     => model('admin/User','form')->items($this->site['query']),
        ]);
        $this->assign('form', $form);
        $this->assign($this->site);
        return $this->fetch();
    }
    public function save()
    {
        $post = xfArrayFilter(input('post.'), ['user_status','user_name','user_mobile','user_email','user_pass','user_roles','user_caps','user_score']);
        $post['user_roles'] = [$post['user_roles']];
        if($id = model('common/User','loglic')->save($post)){
            $this->success('成功','admin/user/index');
        }else{
            $this->error('失败');
        }
    }

    public function delete()
    {
        $result = model('common/User','loglic')->deleteId(input('id/d',0));
        if($result){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }

    public function deletes()
    {
        foreach(input('ids/a') as $key=>$id){
            model('common/User','loglic')->deleteId($id);
        }
        $this->success('成功');
    }
    
    public function edit()
    {
        if(!$id = input('id/d',0)){
            $this->error($this->error,'admin/userindex');
        }
        //查询数据
        $result = model('common/User','loglic')->getId($id);
        if(!$result){
            $this->error($this->error,'admin/user/index');
        }
        //生成表单
        $form = xfFormCreate([
            'name'      => 'adminUserEdit',
            'action'    => xfUrl('admin/user/update'),
            'class'     => 'row',
            'reset'     => '重置',
            'items'     => model('admin/User','form')->items($result),
        ]);
        //加载模板
        $this->assign($result);
        $this->assign('form',$form);
        $this->assign($this->site);
        return $this->fetch();
    }
    public function update()
    {
        $post = xfArrayFilter(input('post.'), ['user_status','user_name','user_mobile','user_email','user_pass','user_roles','user_caps','user_score','user_id']);
        $post['user_roles'] = [$post['user_roles']];
        if($id = model('common/User','loglic')->updateId($post['user_id'],$post)){
            $this->success('成功','admin/user/index');
        }else{
            $this->error('失败');
        }
    }

    //批量审核
    public function normal()
    {
        $result = model('common/User','loglic')->fieldUpdate(input('ids/a'),'user_status','normal');

        $this->success('成功');
    }

    //批量隐藏
    public function hidden()
    {
        $result = model('common/User','loglic')->fieldUpdate(input('ids/a'),'user_status','hidden');

        $this->success('成功');
    }
}